package com.kerrykidz.sms.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.kerrykidz.sms.dao.model.SmsBatchCondEntity;

public class BatchCondUtil {
	private static String smsBatchCondDetail(String condition,int index){
		String[] arry = condition.split(",");
		if(arry.length > 0 && arry.length > index){
			String context =  arry[index];
			if(context != null && StringUtils.isNotBlank(context)){
				return context;
			}
		}
		return null;
	}
	
	public  static boolean isBlank(String... arry){
		boolean flag = true;
		if(arry != null && arry.length > 0){
			for (String str : arry){
				if(str == null || !(StringUtils.isNotBlank(str))){
					flag = false;
					break;
				}
			}
		}
		return flag;
	}
	/***
	 * 处理抽取短信条件
	 * @param entityList
	 * @param map
	 * @date: 2014年9月29日
	 */
	public static  void smsBatchCondWhereMap(List<SmsBatchCondEntity> entityList,Map<String,Object> map){
		if(entityList != null && entityList.size() > 0){
			for(SmsBatchCondEntity cond : entityList){
				
				String condition = cond.getCondDetail();
				String[] arry = new String[0];
				if(StringUtils.isNotBlank(condition)){
					arry =  condition.split("\\|");
				}
					
					/*if(arry != null && arry.length == 0) continue;*/
					
					String a0 = arry.length > 0 ? arry[0]:"";
					String a1 = arry.length > 1 ? arry[1] :"";
					String a2 = arry.length > 2 ? arry[2] :"";
					String a3 = arry.length > 3 ? arry[3] :"";
					String a4 = arry.length > 4 ? arry[4] :"";
					
					if(cond.getProfileType().equals("1") ){ //会员
						String status = smsBatchCondDetail(a0, 0);
						
						String b1 = smsBatchCondDetail(a1, 0);
						String b2 = smsBatchCondDetail(a1, 1);
						
						String r1 = smsBatchCondDetail(a2, 0);
						String r2 = smsBatchCondDetail(a2, 1);
						
						String e1 = smsBatchCondDetail(a3, 0);
						String e2 = smsBatchCondDetail(a3, 1);
						
						String u1 = smsBatchCondDetail(a4, 0);
						
						boolean b = isBlank(b1,b2);
						boolean r = isBlank(r1,r2);
						boolean e = isBlank(e1,e2);
						boolean u = isBlank(u1);
						boolean s = isBlank(status);
						map.put("hy", "true");
						if(b || r || e || u || s ){
							if(b){
								map.put("b1", b1);
								map.put("b2", b2);
							}
							if(r){
								map.put("r1", r1);
								map.put("r2", r2);
							}
							if(e){
								map.put("e1", e1);
								map.put("e2", e2);
							}
							if(u){
								map.put("u1", u1);
							}
							if(s){
								map.put("status", status);
							}
						}
					}else if(cond.getProfileType().equals("2")){//非会员
						String fb1 = smsBatchCondDetail(a0, 0);
						String fb2 = smsBatchCondDetail(a0, 1);
						String fP1 = smsBatchCondDetail(a1, 0);
						String fP2 = smsBatchCondDetail(a1, 1);
						boolean fb = isBlank(fb1,fb2);
						boolean fP = isBlank(fP1,fP2);
						map.put("fhy", "true");
						if(fP || fb){
							if(fb){
								map.put("fb1", fb1);
								map.put("fb2", fb2);
							}
							if(fP){
								map.put("fP1", fP1);
								map.put("fP2", fP2);
							}
						}
						
					}else if(cond.getProfileType().equals("3")){//访客
						String fki1 = smsBatchCondDetail(a0, 0);
						String fki2 = smsBatchCondDetail(a0, 1);
						String fku1 = smsBatchCondDetail(a1, 0);
						boolean fki = isBlank(fki1,fki2);
						boolean fku = isBlank(fku1);
						map.put("fk", "true");
						if(fki || fku){
							if(fki){
								map.put("fki1", fki1);
								map.put("fki2", fki2);
							}
							if(fku){
								map.put("fku1", fku1);
							}
						}
					}else if(cond.getProfileType().equals("4")){//派对
						String pdjb1 = smsBatchCondDetail(a0, 0);
						String pdjb2 = smsBatchCondDetail(a0, 1);
						boolean pdjb = isBlank(pdjb1,pdjb2);
						map.put("pd", "true");
						if(pdjb){
							map.put("pdjb1", pdjb1);
							map.put("pdjb2", pdjb2);
						}
					}
				/*}*/
			}
		}
	}

	/**
	 * 返回分割集合
	 * @param str 
	 * @param comma
	 * @return
	 * @date: 2014年10月8日
	 */
	public static List<String> split(String str,String comma){
		List<String> resultList = new ArrayList<String>();
		if(StringUtils.isNotBlank(str)){
			String[] arryStr = str.split(comma);
			if(arryStr.length > 0){
				for(String s : arryStr){
					if(StringUtils.isNotBlank(s)){
						resultList.add(s);
					}
				}
			}
		}
		return resultList;
	}

	public static void main(String[] args) {
		String[] str = "||||4".split("\\|");
		System.out.println(str.length);
	}
}
